package verins.GUI;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import verins.algrtm.LanzadorAlgoritmos;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.awt.Image;
import javax.swing.JPanel;
/**
 *
 * @author miguel
 */

class PanelImagen extends JPanel{
    private BufferedImage img;
    private double escala;
    public void cargarImagen(String ruta){
        File archivo = new File(ruta);
        try{
            img = (ImageIO.read(archivo));
            escala = Math.min(200.0/img.getHeight(), 200.0/img.getHeight());
        } catch (Exception e){
            JOptionPane.showMessageDialog(null, "Error cargando archivo");
            img=null;
        }
    }

    @Override
    public  void paintComponents(Graphics g){
        if (img==null) return;
        int ancho = (int)(img.getWidth()*escala);
        int alto = (int)(img.getHeight()*escala);
        g.drawImage(img,0,0, ancho, alto, this);
    }
}

public class frmProbarAlgoritmo extends javax.swing.JFrame {

    public frmProbarAlgoritmo() {
        initComponents();
        jPanel1 = new PanelImagen();
        jPanel2 = new PanelImagen();
        getContentPane().add(jPanel1);
        getContentPane().add(jPanel2);
        verins.Utils.Comunes.centrarVentana(this);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblAlgoritmo = new javax.swing.JLabel();
        cmbAlgoritmo = new javax.swing.JComboBox();
        txtImagen1 = new javax.swing.JTextField();
        lblImagen1 = new javax.swing.JLabel();
        btnImagen1 = new javax.swing.JButton();
        lblImagen2 = new javax.swing.JLabel();
        txtImagen2 = new javax.swing.JTextField();
        btnImagen2 = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        btnProbar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Prueba de Algoritmos");
        setResizable(false);

        lblAlgoritmo.setText("Algoritmo");

        cmbAlgoritmo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Huellas - Identificación por Minucias", "Firmas - Identificación por polígonos" }));

        txtImagen1.setEnabled(false);

        lblImagen1.setText("Imagen 1");

        btnImagen1.setText("...");
        btnImagen1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImagen1ActionPerformed(evt);
            }
        });

        lblImagen2.setText("Imagen 2");

        txtImagen2.setEnabled(false);

        btnImagen2.setText("...");
        btnImagen2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImagen2ActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnProbar.setText("Probar Algoritmo");
        btnProbar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProbarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblAlgoritmo)
                                .addGap(36, 36, 36)
                                .addComponent(cmbAlgoritmo, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblImagen1)
                                    .addComponent(lblImagen2))
                                .addGap(51, 51, 51)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtImagen2)
                                    .addComponent(txtImagen1, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnImagen2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnImagen1, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnProbar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAlgoritmo)
                    .addComponent(cmbAlgoritmo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblImagen1)
                    .addComponent(txtImagen1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnImagen1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblImagen2)
                        .addComponent(txtImagen2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnImagen2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnProbar))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed



    private void btnImagen1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImagen1ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Mapa de bits", "bmp");
        fileChooser.setFileFilter(filter);
        fileChooser.setCurrentDirectory(new File(verins.Utils.Comunes.getPathImagenes()));
        int status = fileChooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            txtImagen1.setText(selectedFile.getPath());
            jPanel1.cargarImagen(txtImagen1.getText());
        } else if (status == JFileChooser.CANCEL_OPTION) {
            txtImagen1.setText("");
        }
    }//GEN-LAST:event_btnImagen1ActionPerformed

    @Override
    public void paint(Graphics g){
        paintComponents(g);
        jPanel1.paintComponents(jPanel1.getGraphics());
        jPanel2.paintComponents(jPanel2.getGraphics());
    }

    private void btnImagen2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImagen2ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Mapa de bits", "bmp");
        fileChooser.setFileFilter(filter);
        fileChooser.setCurrentDirectory(new File(verins.Utils.Comunes.getPathImagenes()));
        int status = fileChooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            txtImagen2.setText(selectedFile.getPath());
        } else if (status == JFileChooser.CANCEL_OPTION) {
            txtImagen2.setText("");
        }
    }//GEN-LAST:event_btnImagen2ActionPerformed

    private void btnProbarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProbarActionPerformed
        if (txtImagen1.getText().equals("") || txtImagen2.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Debe ingresar ambos campos");
            return;
        }
        String nombreAlgoritmo="";
        switch (cmbAlgoritmo.getSelectedIndex()){
            case 0: nombreAlgoritmo = "HuellasMinucias";
            case 1: nombreAlgoritmo = "FirmasPoligonos";
        }
        boolean evaluacion = LanzadorAlgoritmos.evaluar(nombreAlgoritmo, txtImagen1.getText(), txtImagen2.getText(), 70);
        if (evaluacion){
            JOptionPane.showMessageDialog(null, "Las imágenes presentan un grado de similitud adecuado.");
        }
        else{
            JOptionPane.showMessageDialog(null, "Las imágenes no son suficientemente similares");
        }
    }//GEN-LAST:event_btnProbarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmProbarAlgoritmo().setVisible(true);
            }
        });
    }
    private PanelImagen jPanel1;
    private PanelImagen jPanel2;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImagen1;
    private javax.swing.JButton btnImagen2;
    private javax.swing.JButton btnProbar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbAlgoritmo;
    private javax.swing.JLabel lblAlgoritmo;
    private javax.swing.JLabel lblImagen1;
    private javax.swing.JLabel lblImagen2;
    private javax.swing.JTextField txtImagen1;
    private javax.swing.JTextField txtImagen2;
    // End of variables declaration//GEN-END:variables

}
